package org.zqh.scaleserver;

import com.jfinal.core.Controller;
import com.jfinal.core.NotAction;
import com.jfinal.plugin.activerecord.Db;
import sun.misc.BASE64Decoder;

import java.io.FileOutputStream;
import java.io.OutputStream;

/**
 * 接收请求，进行处理
 */
public class BdController extends Controller {

    public void index() {
        renderText("scale server");
    }

    /**
     * 上传磅单信息
     */
    public void uploadBd() {

        Object[] paraObj = new Object[21];
        paraObj[0] = get("bdcode");
        paraObj[1] = get("fromname");
        paraObj[2] = get("toname");
        paraObj[3] = get("ingby");
        paraObj[4] = get("carno");
        paraObj[5] = get("dbcode");
        paraObj[6] = get("dbname");
        paraObj[7] = get("matname");
        paraObj[8] = getInt("maozhong");
        paraObj[9] = getInt("jingzhong");
        paraObj[10] = getInt("pizhong");
        paraObj[11] = get("bdtype");
        paraObj[12] = get("delflag");
        paraObj[13] = get("note");
        paraObj[14] = get("erpno");
        paraObj[15] = get("erphid");
        paraObj[16] = get("erpbid");
        paraObj[17] = get("pkstockorg");
        paraObj[18] = get("sendnc");
        paraObj[19] = get("intime");
        paraObj[20] = get("outtime");
        try {
            Db.update("insert into t_bangdan(vbdcode,vfromname,vtoname,vingby,vcarno,vdbcode,vdbname,vmatname," +
                    "nmaozhong,njingzhong,npizhong,vbdtype,vdelflag,vnote,verpno,nerphid,nerpbid," +
                    "pkstockorg,vsendnc,dintime,douttime) values(?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,to_date(?,'yyyymmddhh24miss'),to_date(?,'yyyymmddhh24miss'))", paraObj);
            try {
                Object[] paraPicObj = new Object[5];
                paraPicObj[0] = get("bdcode");
                paraPicObj[1] = get("inpicfile3");
                paraPicObj[2] = get("inpicfile4");
                paraPicObj[3] = get("outpicfile3");
                paraPicObj[4] = get("outpicfile4");
                Db.update("insert into t_bangdan_pic(vbdcode," +
                        "inpicfile3,inpicfile4,outpicfile3,outpicfile4) values(?,?,?,?,?)", paraPicObj);
                renderText("ok");
            }
            catch (Exception e)
            {
                renderText("picerror");
            }
            renderText("ok");
        }
        catch (Exception e)
        {
            renderText("bderror");
        }


    }

    /**
     * 上传磅单信息
     */
    public void insertBdHex() {

        Object[] paraObj = new Object[21];
        paraObj[0] = get("bdcode");
        paraObj[1] = hexStringToString(get("fromname"));
        paraObj[2] = hexStringToString(get("toname"));
        paraObj[3] = hexStringToString(get("ingby"));
        paraObj[4] = hexStringToString(get("carno"));
        paraObj[5] = hexStringToString(get("dbcode"));
        paraObj[6] = hexStringToString(get("dbname"));
        paraObj[7] = Double.valueOf(get("maozhong"));
        paraObj[8] = Double.valueOf(get("jingzhong"));
        paraObj[9] = Double.valueOf(get("pizhong"));
        paraObj[10] = hexStringToString(get("bdtype"));
        paraObj[11] = hexStringToString(get("delflag"));
        paraObj[12] = hexStringToString(get("note"));
        paraObj[13] = hexStringToString(get("erpno"));
        paraObj[14] = hexStringToString(get("erphid"));
        paraObj[15] = hexStringToString(get("erpbid"));
        paraObj[16] = hexStringToString(get("outgby"));
        paraObj[17] = hexStringToString(get("matname"));
        paraObj[18] = hexStringToString(get("sendnc"));
        paraObj[19] = get("intime");
        paraObj[20] = get("outtime");

        Db.update("insert into t_bangdan(vbdcode,vfromname,vtoname,vingby,vcarno,vdbcode,vdbname," +
                "nmaozhong,njingzhong,npizhong,vbdtype,vdelflag,vnote,verpno,nerphid,nerpbid," +
                "voutgby,vmatname,vsendnc,dintime,douttime) values(?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,to_date(?,'yyyymmddhh24miss'),to_date(?,'yyyymmddhh24miss'))", paraObj);
        renderText("ok");
    }

    /**
     * 更新磅单信息
     */
    public void updateBdHex() {
        String bdcode = get("bdcode");
        double pizhong = Double.valueOf(get("pizhong"));
        double maozhong = Double.valueOf(get("maozhong"));
        double jingzhong = Double.valueOf(get("jingzhong"));
        String outtime = get("outtime");
        String outgby = hexStringToString(get("outgby"));

        Object[] paraObj = new Object[6];
        paraObj[0] = pizhong;
        paraObj[1] = maozhong;
        paraObj[2] = jingzhong;
        paraObj[3] = outtime;
        paraObj[4] = outgby;
        paraObj[5] = bdcode;
        Db.update("update t_bangdan set npizhong=?,nmaozhong=?,njingzhong=?,douttime=to_date(?,'yyyymmddhh24miss'),voutgby=? where vbdcode=?", paraObj);
        renderText("ok");
    }

    /**
     * 更新磅单信息
     */
    public void uploadPic() {

        String bdcode = get("bdcode");
        String inout = get("inout");
        String pic1 = get("pic1");
        String picName1 = "";
        if (null != pic1 && pic1.length() > 0) {
            picName1 = "D:\\zqh\\bdpic\\" + bdcode + inout + "1.jpg";
            GenerateImage(pic1, picName1);
        }
        String pic2 = get("pic2");
        String picName2 = "";
        if (null != pic2 && pic2.length() > 0) {
            picName2 = "D:\\zqh\\bdpic\\" + bdcode + inout + "2.jpg";
            GenerateImage(pic2, picName2);
        }
        String pic3 = get("pic3");
        String picName3 = "";
        if (null != pic3 && pic3.length() > 0) {
            picName3 = "D:\\zqh\\bdpic\\" + bdcode + inout + "3.jpg";
            GenerateImage(pic3, picName3);
        }
        String pic4 = get("pic4");
        String picName4 = "";
        if (null != pic4 && pic4.length() > 0) {
            picName4 = "D:\\zqh\\bdpic\\" + bdcode + inout + "4.jpg";
            GenerateImage(pic4, picName4);
        }

        Object[] paraObj = new Object[5];
        paraObj[0] = picName1;
        paraObj[1] = picName2;
        paraObj[2] = picName3;
        paraObj[3] = picName4;
        paraObj[4] = bdcode;

        if (inout.equalsIgnoreCase("in")) {
            Db.update("insert into t_bangdan_pic(inpicfile1,inpicfile2,inpicfile3,inpicfile4,vbdcode) values(?,?,?,?,?)", paraObj);
            renderText("ok");
        } else {
            Db.update("update t_bangdan_pic set outpicfile1=?,outpicfile2=?,outpicfile3=?,outpicfile4=? where vbdcode=?", paraObj);
            renderText("ok");
        }
    }

    /**
     * 更新磅单信息
     */
    @NotAction
    public void updateBd() {
        String bdcode = get("bdcode");
        int pizhong = getInt("pizhong");
        int maozhong = getInt("maozhong");
        int jingzhong = getInt("jingzhong");
        String outtime = get("outtime");
        String outgby = get("outgby");

        Object[] paraObj = new Object[6];
        paraObj[0] = pizhong;
        paraObj[1] = maozhong;
        paraObj[2] = jingzhong;
        paraObj[3] = outtime;
        paraObj[4] = outgby;
        paraObj[5] = bdcode;
        Db.update("update t_bangdan set npizhong=?,nmaozhong=?,njingzhong=?,douttime=to_date(?,'yyyymmddhh24miss'),voutgby=? where vbdcode=?", paraObj);
        renderText("ok");
    }

    /**
     * 16进制转换成为string类型字符串
     *
     * @param s
     * @return
     */
    @NotAction
    public String hexStringToString(String s) {
        if (s == null || s.equals("")) {
            return null;
        }
        s = s.replace(" ", "");
        byte[] baKeyword = new byte[s.length() / 2];
        for (int i = 0; i < baKeyword.length; i++) {
            try {
                baKeyword[i] = (byte) (0xff & Integer.parseInt(s.substring(i * 2, i * 2 + 2), 16));
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        try {
            s = new String(baKeyword, "UTF-8");
            new String();
        } catch (Exception e1) {
            e1.printStackTrace();
        }
        return s;
    }

    @NotAction
    public static boolean GenerateImage(String imgStr, String imgFilePath) {// 对字节数组字符串进行Base64解码并生成图片
        if (imgStr == null) // 图像数据为空
            return false;
        String replace = imgStr.replace(" ", "+");
        BASE64Decoder decoder = new BASE64Decoder();
        try {
            // Base64解码
            byte[] bytes = decoder.decodeBuffer(replace);
            for (int i = 0; i < bytes.length; ++i) {
                if (bytes[i] < 0) {// 调整异常数据
                    bytes[i] += 256;
                }
            }
            // 生成jpeg图片
            OutputStream out = new FileOutputStream(imgFilePath);
            out.write(bytes);
            out.flush();
            out.close();
            return true;
        } catch (Exception e) {
            return false;
        }
    }
}


